import java.util.Scanner;

public class hash {
    /**
     * 给定一个仅由小写字母组成的字符串.现在找出一个位置,
     * 删掉那个字母后,字符串变成回文,如果给定的字符串已经是一个回文,那么输出-1
     * 例如:输入:3 aaab baa aaa
     * 输出:3  0  -1
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int num = scanner.nextInt();
        while (num > 0) {
            StringBuffer sb = new StringBuffer(scanner.next());

            int[] start = new int[1];
            int[] end = new int[1];
            if (isPalindrom(sb,start,end)) {
                System.out.println(-1);
            }else {
                sb.deleteCharAt(end[0]);
                if (isPalindrom(sb,null,null)) {
                    System.out.println(end[0]);
                }else {
                    System.out.println(start[0]);
                }
            }
            num--;
        }
    }

    private static boolean isPalindrom(StringBuffer sb, int[] start, int[] end) {
        int i = 0;
        int j = sb.length()-1;
        boolean result = true;
        while (i <= j) {
            if (sb.charAt(i) != sb.charAt(j)) {//不是回文
                result = false;
                break;
            }
            i++;
            j--;
        }
        if (start != null) start[0] = i;
        if (end != null) end[0] = j;
        return result;
    }

}
